package com.syxp.pda.service.base.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.util.ArraysBuilder;
import com.syxp.pda.dao.base.BaseDutyRuleDao;
import com.syxp.pda.dao.base.DepartmentDao;
import com.syxp.pda.dao.base.DutyRuleDao;
import com.syxp.pda.entity.base.BaseDutyRule;
import com.syxp.pda.entity.base.Department;
import com.syxp.pda.entity.base.DutyRule;
import com.syxp.pda.service.base.DutyRuleService;

@Transactional
public class DutyRuleServiceImpl implements DutyRuleService{

	private DepartmentDao departmentDao ;
	
	private DutyRuleDao dutyRuleDao ;
	
	private BaseDutyRuleDao baseDutyRuleDao ;
	
	public BaseDutyRuleDao getBaseDutyRuleDao() {
		return baseDutyRuleDao;
	}

	public void setBaseDutyRuleDao(BaseDutyRuleDao baseDutyRuleDao) {
		this.baseDutyRuleDao = baseDutyRuleDao;
	}

	public DepartmentDao getDepartmentDao() {
		return departmentDao;
	}

	public void setDepartmentDao(DepartmentDao departmentDao) {
		this.departmentDao = departmentDao;
	}

	
	public DutyRuleDao getDutyRuleDao() {
		return dutyRuleDao;
	}

	public void setDutyRuleDao(DutyRuleDao dutyRuleDao) {
		this.dutyRuleDao = dutyRuleDao;
	}

	@Override
	public Department loadDutyRuleInfo(int departmentID) {
		// TODO Auto-generated method stub
		Department department = departmentDao.get(departmentID) ;
		if(department.getBaseDutyRule()==null){
			department.setClassDutys(ArraysBuilder.buildSet(department.getClassDutys())) ;
			department.setDutys(ArraysBuilder.buildSet(department.getDutys())) ;
			return department ;
		}else{
			DutyRule dutyRule = new DutyRule() ;
			dutyRule.setDepartmentID(departmentID) ;
			QueryFilter queryFilter = new QueryFilter() ;
			queryFilter.addExample(dutyRule) ;
			List<String> orderByList = new ArrayList<String>() ;
			orderByList.add("duRuOrder") ;
			queryFilter.setOrderProperty(orderByList) ;
			List<DutyRule> dutyRules = dutyRuleDao.getAll(queryFilter) ;
			department.setDutyRules(dutyRules) ;
			department.setClassDutys(ArraysBuilder.buildSet(department.getClassDutys())) ;
			department.setDutys(ArraysBuilder.buildSet(department.getDutys())) ;
			return department ;
		}
	}

	@Override
	public void saveBaseDutyRule(int departmentID,BaseDutyRule baseDutyRule,List<DutyRule> dutyRules) {
		
		Department department = departmentDao.get(departmentID) ;
		BaseDutyRule baseDR = department.getBaseDutyRule() ;
		if(baseDR!=null){
			baseDR.setBaseDate(baseDutyRule.getBaseDate()) ;
			baseDR.setCircleDays(baseDutyRule.getCircleDays()) ;
			baseDR.setSpan(baseDutyRule.getSpan()) ;
			baseDutyRuleDao.update(baseDR) ;
			Object[] o = {departmentID} ;
			dutyRuleDao.executeHql("delete from DutyRule dutyRule where dutyRule.departmentID=?", o) ;
		}else{
			department.setBaseDutyRule(baseDutyRule) ;
			departmentDao.update(department) ;
		}
		
		for(DutyRule dutyRule : dutyRules){
			dutyRuleDao.save(dutyRule); 
		}
	}
	
	
}
